#include "show_messagelist.h"
#include "ui_show_messagelist.h"
#include "studentstore.h"
#include <vector>
#include "utils.h"

using namespace studentstore;
using namespace utils;

show_messageList::show_messageList(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::show_messageList) {
    ui->setupUi(this);
    QStringList header;
    ui->tableWidget->setColumnCount(5);
    header << "时间" << "发送者" << "发送者学号" << "接收者" << "内容";
    ui->tableWidget->setHorizontalHeaderLabels(header);
}

show_messageList::~show_messageList() {
    delete ui;
}

void show_messageList::getData(Student *stu) {
    this->stu = stu;
}

void show_messageList::on_pushButton_clicked() {
    loadMessageList();
}

void show_messageList::loadMessageList() {

    vector<Message*>msgList = readMessage(stu->ID);

    for (int i = 0; i < msgList.size(); i++) {      //展示信息

        ui->tableWidget->setRowCount(msgList.size());   //设置表格行数

        //发送时间
        time_t timep = msgList[i]->sendTime;
        //格式化当前时间
        QString fomatStr;
        tm *timem = localtime(&timep);
        QString year = QString::number(1900 + timem->tm_year);
        QString month = QString::number(1 + timem->tm_mon);
        if((1 + timem->tm_mon) < 10)
            month = "0" + month;
        QString day = QString::number(timem->tm_mday);
        if(timem->tm_mday < 10)
            day = "0" + day;
        QString hour = QString::number(timem->tm_hour);
        if(timem->tm_hour < 10)
            hour = "0" + hour;
        QString min = QString::number(timem->tm_min);
        if(timem->tm_min < 10)
            min = "0" + min;
        fomatStr = year + "-" + month + "-" + day + " " + hour + ":" + min;
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(fomatStr));

        QString tempCell;
        //发送者
        if(msgList[i]->receiver_id == -2) { //公告
            ui->tableWidget->setItem(i, 1, new QTableWidgetItem("管理员"));
            ui->tableWidget->setItem(i, 2, new QTableWidgetItem("公告"));
        } else if(msgList[i]->sender_id == -2) {
            ui->tableWidget->setItem(i, 1, new QTableWidgetItem("管理员"));
            ui->tableWidget->setItem(i, 2, new QTableWidgetItem("私聊"));
        }else if(msgList[i]->sender_id == -1) {
            ui->tableWidget->setItem(i, 1, new QTableWidgetItem("超级管理员"));
            ui->tableWidget->setItem(i, 2, new QTableWidgetItem("私聊"));
        } else if(msgList[i]->sender_id == stu->ID) {
            Student* user = search<Student>(msgList[i]->sender_id, studentDatPath);
            ui->tableWidget->setItem(i, 1, new QTableWidgetItem("我"));
            tempCell = QString(QString::fromLocal8Bit(user->stu_id));
            ui->tableWidget->setItem(i, 2, new QTableWidgetItem(tempCell));
        } else {
            Student* user = search<Student>(msgList[i]->sender_id, studentDatPath);
            tempCell = QString(QString::fromLocal8Bit(user->name));
            ui->tableWidget->setItem(i, 1, new QTableWidgetItem(tempCell));
            tempCell = QString(QString::fromLocal8Bit(user->stu_id));
            ui->tableWidget->setItem(i, 2, new QTableWidgetItem(tempCell));
        }

        //接收者
        if(msgList[i]->receiver_id == -2) { //公告
            ui->tableWidget->setItem(i, 3, new QTableWidgetItem("所有人"));
        } else if(msgList[i]->receiver_id == stu->ID) {
            ui->tableWidget->setItem(i, 3, new QTableWidgetItem("我"));
        } else if(msgList[i]->receiver_id == -1){
            ui->tableWidget->setItem(i, 3, new QTableWidgetItem("超级管理员"));
        }else {
            Student* user = search<Student>(msgList[i]->receiver_id, studentDatPath);
            tempCell = QString(QString::fromLocal8Bit(user->name));
            ui->tableWidget->setItem(i, 3, new QTableWidgetItem(tempCell));
        }

        //messageBody
        tempCell = QString(QString::fromLocal8Bit(msgList[i]->messageBody));
        ui->tableWidget->setItem(i, 4, new QTableWidgetItem(tempCell));

    }
}
